package com.budwk.app.sqfwV2.service.impl;

import com.budwk.app.common.config.AppException;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.ProblemParamDTO;
import com.budwk.app.sqfwV2.dto.entity.SqfwProblemInfo;
import com.budwk.app.sqfwV2.service.ProblemInfoService;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.IocBean;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@IocBean(args = {"refer:dao"})
public class ProblemInfoServiceImpl extends BaseServiceImpl<SqfwProblemInfo> implements ProblemInfoService {
    public ProblemInfoServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public List<SqfwProblemInfo> findByCode(String problemCode) {
        Cnd cnd = Cnd.NEW();
        SqlExpressionGroup sqlExpressionGroup = cnd.where();
        sqlExpressionGroup.andEquals(SqfwProblemInfo::getProblemCode, problemCode).andEquals(SqfwProblemInfo::getDeleted, false);
        return this.query(cnd);
    }

    @Override
    public List<SqfwProblemInfo> listByCodes(List<String> problemCodes) {
        Cnd cnd = Cnd.NEW();
        SqlExpressionGroup sqlExpressionGroup = cnd.where();
        sqlExpressionGroup.andIn(SqfwProblemInfo::getProblemCode, problemCodes.toArray(new String[0]))
                .andEquals(SqfwProblemInfo::getDeleted, false);
        return this.query(cnd);
    }

    @Override
    public SqfwProblemInfo findByCodeAndFields(String problemCode, String fields) {
        Cnd cnd = Cnd.NEW();
        SqlExpressionGroup sqlExpressionGroup = cnd.where();
        sqlExpressionGroup.andEquals(SqfwProblemInfo::getProblemCode, problemCode)
                .andEquals(SqfwProblemInfo::getFields, fields)
                .andEquals(SqfwProblemInfo::getDeleted, false);
        return this.fetch(cnd);
    }

    @Override
    public SqfwProblemInfo insertProblem(SqfwProblemInfo problemInfo) {
        SqfwProblemInfo info = findByCodeAndFields(problemInfo.getProblemCode(), problemInfo.getFields());
        if (info != null) {
            throw new AppException("问题编码已存在，请勿重新添加！");
        }
        problemInfo.setDeleted(false).setCreateDate(LocalDateTime.now()).setCreateId(SecurityUtil.getUserId())
                .setUpdateDate(LocalDateTime.now()).setUpdateId(SecurityUtil.getUserId());
        this.insert(problemInfo);
        return problemInfo;
    }

    @Override
    public List<ProblemParamDTO> getAllDefault(String problemCode) {
        List<SqfwProblemInfo> infos = findByCode(problemCode);
        if (infos.size() == 0) {
            throw new AppException("请先初始化问题！");
        }
        List<ProblemParamDTO> list = new ArrayList<>();
        for (SqfwProblemInfo info : infos) {
            ProblemParamDTO p = new ProblemParamDTO();
            p.setFields(info.getFields()).setKey(info.getDefaultValue()).setRiskDesc(info.getRiskDesc()).setRiskCode(String.valueOf(info.getRiskCode()));
            list.add(p);
        }
        return list;
    }
}
